package com.semlab.client.widgets;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Style;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.SimplePanel;
import com.semlab.client.bundle.Resources;

import java.util.ArrayList;

/**
 * @author Slobodan
 */
public abstract class ListPager extends Container<HorizontalPanel> {

    //    private Hyperlink currentPageLink;
    private int currentPage = -1;
    private int total;
    private int page;
    private HorizontalPanel numbers;
    private ArrayList<Hyperlink> links;
    private Image prev;
    private SimplePanel prevHolder;
    private SimplePanel nextHolder;
    private Image next;

    private int numOfNums = 5;

    public ListPager(int page) {
        super(new HorizontalPanel());
        this.page = page;
        numbers = new HorizontalPanel();
        numbers.setWidth("100px");
        links = new ArrayList<Hyperlink>();
        prev = new Image(Resources.INSTANCE.retract());
        next = new Image(Resources.INSTANCE.expand());
        prev.setVisible(false);
        next.setVisible(false);
        prevHolder = new SimplePanel();
        nextHolder = new SimplePanel();
        prevHolder.add(prev);
        addWidget(prevHolder);
//        WidgetUtils.glue(getTopContainer(), "10px");
        addWidget(numbers);
        numbers.getElement().getStyle().setProperty("margin","0px 10px");
//        WidgetUtils.glue(getTopContainer(), "10px");
        nextHolder.add(next);
        addWidget(nextHolder);
        prevHolder.setWidth("28px");
        nextHolder.setWidth("28px");
        prev.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                if (currentPage - 1 >= 0) {
                    goToPage(currentPage - 1);
                    next.setVisible(true);
                }


            }
        });
        next.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                if (currentPage + 1 < links.size()) {
                    goToPage(currentPage + 1);
                    prev.setVisible(true);
                }

            }
        });
        getTopContainer().setHeight("20px");
        prev.addStyleName(Resources.INSTANCE.css().hand());
        prev.getElement().getStyle().setMarginTop(2, Style.Unit.PX);
        next.getElement().getStyle().setMarginTop(2, Style.Unit.PX);
        next.addStyleName(Resources.INSTANCE.css().hand());
    }

    public void setTotal(int total) {
        GWT.log(total+"");
        if (total != this.total) {
            this.total = total;
            numbers.clear();
            int num = total / page;
            GWT.log("num="+num);
            if (total % page > 0) {
                num++;
                GWT.log("tp="+total%page);
            }
            for (int i = 0; i < num; i++) {
                final int finalI = i + 1;
                final Hyperlink link = new Hyperlink(String.valueOf(finalI));
                link.addClickHandler(new ClickHandler() {
                    @Override
                    public void onClick(ClickEvent event) {
                        showSubset((finalI - 1) * page, page);
                        goToPage(finalI - 1);
                    }
                });
                link.getElement().getStyle().setProperty("margin", "0px 5px");
                links.add(link);
            }
            if(links.size() > 1){
            for (int i = 0; i < numOfNums; i++) {
                if(i< links.size())
                numbers.add(links.get(i));
//                WidgetUtils.glue(numbers, "10px");
            }
            prev.setVisible(false);
            next.setVisible(true);
            }else{
                prev.setVisible(false);
                next.setVisible(false);
            }
        }
        Hyperlink link = links.get(currentPage);
        link.getElement().getStyle().setTextDecoration(Style.TextDecoration.UNDERLINE);
    }

    public abstract void showSubset(int offset, int num);

    public void goToPage(int i) {
        showSubset(i * page, page);
        if (currentPage != -1) {
            Hyperlink cur = links.get(currentPage);
            cur.getElement().getStyle().setTextDecoration(Style.TextDecoration.NONE);
            if (numbers.getWidgetIndex(links.get(i)) == -1) {
                //move links

                //if next
                if (currentPage < i) {
                    GWT.log("next cur=" + currentPage + " i=" + i);
                    numbers.remove(links.get(currentPage - numOfNums + 1));
//                    WidgetUtils.glue(numbers, "10px");
                    numbers.add(links.get(i));
                }

                //if previous
                if (currentPage > i) {
                    GWT.log("prev cur=" + currentPage + " i=" + i);
                    numbers.remove(links.get(currentPage + numOfNums - 1));
//                    WidgetUtils.insertGlue(numbers, "10px");
                    numbers.insert(links.get(i), 0);
                }

                //else
            }
        }
        currentPage = i;
        if (currentPage <= 0) {
            prev.setVisible(false);
        }
        if (currentPage >= links.size() - 1) {
            next.setVisible(false);
        }
    }
}
